package com.example.day1210_section_information.slice;

import com.example.day1210_section_information.Provider.GuidePageProvide;
import com.example.day1210_section_information.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderIndicator;
import ohos.agp.components.element.ShapeElement;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

/**
 * 引导页
 */

public class GuidePageSlice extends AbilitySlice {


    private MyEvenHandle handle = new MyEvenHandle(EventRunner.create("jishi"));

    // even
    private static final int TIMER = 1;
    // 当前图片位置
    private int mIndex = 0;

    private PageSlider mPageSlider;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_guide_page);

        initCompent();

        handle.sendEvent(TIMER, 2000);
    }

    /**
     * 初始化组件
     */
    private void initCompent() {
        mPageSlider = findComponentById(ResourceTable.Id_page_guide);
        mPageSlider.setProvider(new GuidePageProvide(getContext()));
//   点击进入首页
        Button pageBtn = findComponentById(ResourceTable.Id_enter_page_btn);
        pageBtn.setClickedListener(component -> enterPage());
// 添加一个滑动事件监听
        mPageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
//  页面正在滑动

            }

            @Override
            public void onPageSlideStateChanged(int i) {
// 页面发生变化
            }

            @Override
            public void onPageChosen(int i) {
// 新的页面
                boolean isShow = i == 2;
                pageBtn.setVisibility(isShow ? Component.VISIBLE : Component.INVISIBLE);
            }
        });

        PageSliderIndicator pageIndicator = findComponentById(ResourceTable.Id_guide_indicator);
        ShapeElement norShapeElement = new ShapeElement(this, ResourceTable.Graphic_nor_guide_dicator);
        ShapeElement selectShapeElement = new ShapeElement(this, ResourceTable.Graphic_select_guide_dicator);
//  设置样式
        pageIndicator.setItemElement(norShapeElement, selectShapeElement);
        // 关联
        pageIndicator.setPageSlider(mPageSlider);
        // 组件之间偏移量
        pageIndicator.setItemOffset(50);
    }

    /**
     * 进入首页
     */
    private void enterPage() {

        Preferences isGuide = new DatabaseHelper(getContext()).getPreferences("key_guide");
        // 表示已经进入过引导页
        isGuide.putBoolean("is_guide_show", true);
        isGuide.flush();

        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withBundleName(getBundleName())
                .withAbilityName(getBundleName() + ".MainAbility")
                .build();
        intent.setOperation(operation);
        startAbility(intent);

//        present(new MainAbilitySlice(),new Intent());
        terminate();
    }

    /**
     * 每隔两个自动翻到下一个引导页
     */
    private class MyEvenHandle extends EventHandler {

        public MyEvenHandle(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {

            super.processEvent(event);
            int eventId = event.eventId;

            if (eventId == TIMER) {
                mIndex++;
                getUITaskDispatcher().syncDispatch(() -> mPageSlider.setCurrentPage(mIndex));
                if (mIndex < 2) {
                    handle.sendEvent(TIMER, 1000);
                }
            }

        }


    }

}
